/*
 * Copyright 2013 Solace Systems, Inc.
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.solacesystems.tools.ant;

import static org.junit.Assert.*;

import com.solacesystems.tools.ant.Location;
import org.junit.Test;
import org.xml.sax.Locator;

public class LocationTests {

	@Test
	public void testConstructorDefault() {
		Location loc = new Location();
		assertNotNull(loc);
		assertNotNull(loc.path);
	}
	
	@Test
	public void testConstructorVerbose() {
		Location loc = new Location("test", 10, 20);
		assertNotNull(loc);
		assertEquals("test", loc.path);
		assertEquals(10, loc.line);
		assertEquals(20, loc.column);
	}
	
	@Test
	public void testConstructorUnknown() {
		Location loc = new Location(null, -1, -2);
		assertNotNull(loc);
		assertEquals("<unknown>", loc.path);
		assertEquals(-1, loc.line);
		assertEquals(-2, loc.column);
	}
	
	@Test
	public void testEqualsUnknown() {
		Location a = new Location(null, -1, -1);
		Location b = new Location(null, -1, -1);
		
		assertFalse(a.equals(b));
		assertFalse(b.equals(a));
	}
	
	@Test
	public void testEqualsUnknown2() {
		Location a = new Location("file.html", -1, -1);
		Object b = new Object();
		
		assertFalse(a.equals(b));
		assertFalse(b.equals(a));
	}
	
	@Test
	public void testEqualsUnknown3() {
		Location a = new Location("file.html", -1, -1);
		Location b = new Location(null, -1, -1);
		
		assertFalse(a.equals(b));
		assertFalse(b.equals(a));
	}
	
	@Test
	public void testEqualsObject() {
		Location a = new Location("file.html", 10, 100);
		Location b = new Location("file.html", 10, 100);
	
		assertEquals(a, a);
		assertEquals(a, b);
		assertEquals(b, a);
	}
	
	@Test
	public void testEqualsObject2() {
		Location a = new Location("file.html", 10, 100);
		Location b = new Location("banana.html", 10, 100);
	
		assertFalse(a.equals(b));
		assertFalse(b.equals(a));
	}
	
	@Test
	public void testEqualsObject3() {
		Location a = new Location("file.html", 10, 100);
		Location b = new Location("file.html", 11, 100);
	
		assertFalse(a.equals(b));
		assertFalse(b.equals(a));
	}
	
	@Test
	public void testEqualsObject4() {
		Location a = new Location("file.html", 10, 100);
		Location b = new Location("file.html", 10, 101);
	
		assertFalse(a.equals(b));
		assertFalse(b.equals(a));
	}

	@Test
	public void testFromLocator() {
		Locator locator = new Locator() {
			@Override
			public String getSystemId() {
				return "system.html";
			}
			
			@Override
			public String getPublicId() {
				return "public.html";
			}
			
			@Override
			public int getLineNumber() {
				return 100;
			}
			
			@Override
			public int getColumnNumber() {
				return 10;
			}
		};
		
		Location location = Location.fromLocator(locator);
		
		assertEquals(100, location.line);
		assertEquals(10, location.column);
		assertEquals("system.html", location.path);
	}
	
	@Test
	public void testHashCode() {
		Location a = new Location("file.html", 10, 100);
		Location b = new Location("file.html", 10, 100);
		
		assertEquals(a.hashCode(), b.hashCode());
	}
	
	@Test
	public void testToString() {
		Location a = new Location("banana.html", 10, 100);
		assertTrue(a.toString().contains("banana.html"));
	}

}
